<?php

require_once( "MDB2.php" );

class Post {
	
	public $postId;
	public $parentPostId;
	public $title;
	public $time;
	public $modtime;
	public $body;
	public $posterId;
	
	private $children;
	
	public function getNumberOfChildren() {
		return count( $this->getChildren() );
	}
	
	public function getChildren() {
		return array();
	}
	
}

class Category {
	public $name;
	
	public function getPosts() {
		
	}
}

class PostDAO {
	private $db;
	
	public function __construct( $db = null ) {
		if( $db == null ) {
			$db = MDB2::connect("pgsql://phpfaces_info@localhost/phpfaces_info");
		}
		
		$this->db = $db;	
	}
	
	public function getChildren( $parentId = null ) {
		$posts = array();
		
		$sql = "SELECT * FROM forum_posts";
		$params = array();
		if( $parentId == null ) {
			$sql .= " WHERE parent_post_id is null";
		} else {
			$sql .= " WHERE parent_post_id = ?";
			$params[] = $parentId;
		}
		
		$res = $this->db->query( $sql, $params );
		while ( $row = $res->fetchRow( MDB2_FETCHMODE_ASSOC ) ) {
			$p = new Post();
			$p->postId = $row["post_id"];
			$p->parentPostId = $row["parent_post_id"];
			$p->title = $row["title"];
			$p->time = $row["time"];
			$p->modtime = $row["modtime"];
			$p->body = $row["body"];
			$p->posterId = $row["poster_id"];
			
			$posts[] = $p;
		}
		
		return $posts;
	}
	
}

class ThreadBean {
	public function __construct() {
		$db = MDB2::connect("pgsql://phpfaces_info@localhost/phpfaces_info");
		$this->postDAO = new PostDAO( $db );
	}
	
}

class ForumBean {
	
	private $selectedCategory;
	private $selectedPost;
	
	private $postDAO;
	
	public function __construct() {
		$db = MDB2::connect("pgsql://phpfaces_info@localhost/phpfaces_info");
		$this->postDAO = new PostDAO( $db );
	}
	
	public function getCategories() {
		
	}
	
	private $posts;
	public function getPosts() {
		if( $this->posts == null ) {
			$this->posts = $this->postDAO->getChildren( null );
		}
		return $this->posts;
	}
	
	public function openCategory() {
		
		return "view-cateogry";
	}
	
	public function openPost() {
		return "open-post";
	}
}


?>